Flags
Earlier, I said that commands in Bash/Zsh are like functions in JavaScript. The analogy breaks down a bit when it comes to flags.
Flags are modifiers that tweak the behaviour of commands in predefined ways.
For example, let's look at the rm
command. This command allows us to delete individual files:
We don't get any sort of confirmation, but if we check, the theme-song.mp3
file has indeed been deleted.
If you try and use the rm
command on a directory, you'll get an error:
By default, rm
can only remove individual files, but we can change this rule with the r
flag:
The r
flag stands for “recursive”. It will delete everything inside the stuff
directory, anything inside the directories inside the stuff
directory, anything inside directories inside the directories inside the stuff
directory, and so on.
You might also run into some file permission issues. For that reason, the f
flag (Force) is commonly used as well. We can group multiple flags with a single dash, like this:
Flags take many shapes and sizes. By convention, it's common for flags to have a short form (eg. -f
) and a long form (--force
). The long form typically uses two dashes, and uses whole words instead of individual letters.
Let's look at one more example. the ls
command we saw earlier is commonly called with two flags:
- The
l
flag, “long”, which prints the directory contents in a detailed list with metadata. - The
a
flag, "all", which'll include hidden files and directories.
This changes the output considerably:
There's a lot of noise here, including the ridiculously-obfuscated permission glyphs. But some of the metadata, like the dates that show when a file was last updated, can be useful!